import com.nimbusds.jose.jwk.JWK;
import org.apache.log4j.Logger;
import org.bdware.irp.client.IrpClientImpl;
import org.bdware.irp.exception.IrpClientException;
import org.bdware.irp.irplib.util.GlobalUtils;
import org.bdware.irp.stateinfo.DoStateInfo;
import org.bdware.irp.stateinfo.StateInfoBase;
import org.bdware.irp.stateinfo.UserStateInfo;
import org.bdware.irs.backend.dto.Organization;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class IrpClientTestForGrs {

    Logger logger = Logger.getLogger(IrpClientTestForGrs.class);
    public String url = "tcp://39.104.205.122:2642";
    public IrpClientImpl client;
    boolean waitForResp = true;

    @Before
    public void getIrpClient() throws Exception{
/*        if (Security.getProvider("BC") == null) {
            Security.addProvider(new BouncyCastleProvider());
        }
        try {
            GlobalCertifications.loadKeysFromJKS("keys/dou.TEST.keystore","123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
        GlobalIrpClient.useGeneralIrpClient(GlobalCertifications.jwKeyPair
                , GlobalConfigurations.userID
                , GlobalConfigurations.IRSAddress);*/
/*        JWK jwk = new ECKeyGenerator(Curve.P_256)
                .keyUse(KeyUse.SIGNATURE) // indicate the intended use of the key
                .keyID(UUID.randomUUID().toString()) // give the key a unique ID
                .generate();*/
        try{
            JWK jwk = GlobalUtils.loadKeysFromJWKFile("./../keys/jwk/dou.TEST.jwk");
            logger.info("load the jwk finish");
            client = new IrpClientImpl(jwk, "clientTest", url);
            //client.setAddress(irsAddress);
            client.connect(url);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @After
    public void waitForResp() throws InterruptedException {
        while (waitForResp){
            Thread.sleep(1000*5);
        }
        client.close();
    }



/*    @Test
    public void testVerifyIrs() {
        boolean result = client.verifyIrsServer();
        if (result == true){
            logger.info("verify success!");
        }else
        {
            logger.info("verify failed!");
        }
        waitForResp = false;
    }*/




   @Test
    public void resolve(){
        try{
            StateInfoBase stateInfo = client.resolvePrefix("jy.xj.qb.gkqb/dou.ADMIN");
            if(stateInfo == null){
                logger.info("Resolve failed!");
                waitForResp = false;
                return;
            }
            logger.info(stateInfo.getIdentifier());
            logger.info(stateInfo.getHandleValues());
        }catch(IrpClientException e){
            e.printStackTrace();
        }
        waitForResp = false;
    }


    @Test
    public void createOrg(){
        try{
            Organization organization = new Organization();
            organization.setDoi("aibd.liuyi/dou.ADMIN");
            organization.setName("aibd.liuyi");
            organization.setJwkKey("");
            organization.setProtocol("");
            organization.setServiceAddress("tcp://127.0.0.1:2641");
            organization.setStatus("待审核");
            organization.setPrefix("aibd.liuyi");
            StateInfoBase stateInfo = organization.toStateInfoBase1();
            boolean result = client.allocatePrefix(stateInfo);
        }catch(IrpClientException e){
            e.printStackTrace();
        } finally {
            client.close();
        }

    }

}
